package cn.tedu.property_management_system.controller.ExceptionHandler;

import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.utils.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static cn.tedu.property_management_system.utils.JsonResult.State.*;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public JsonResult ServiceExceptionHandle(ServiceException e){
        return JsonResult.fail(e);
    }

    @ExceptionHandler(BadCredentialsException.class)
    public JsonResult ServiceExceptionHandle(BadCredentialsException e){
        return JsonResult.fail(e.getMessage(),BAD_REQUEST);
    }

    @ExceptionHandler(AccessDeniedException.class)
    public JsonResult AccessDeniedException(AccessDeniedException e){
        return JsonResult.fail("您当前没有权限,请联系管理员", FORBIDDEN);
    }

    @ExceptionHandler(Throwable.class)
    public JsonResult unknowExceptionHandle(Throwable e){
        e.printStackTrace();
        return JsonResult.fail("服务器开小差了", UNKNOW_EXCEPTION);
    }
}
